//获取音频上下文
const backgroundAudioManager = wx.getBackgroundAudioManager();
const db = wx.cloud.database();
Page({

	data: {
		videoIndex: null,
    page:1,
    category:null,
		videoList: []
	},
    /**
     * 生命周期函数--监听页面加载
     */
	onLoad: function (options) {
		//加载数据
    let that = this;
		//加载数据
    let category = parseInt(options.id);
    this.setData({ category: category});
    let filter = { category: category};

    this.findListPage();
	},

	//播放视频
	videoPlay(event) {
		var length = this.data.videoList.length;
		var index = event.currentTarget.dataset['index'];

		if (!this.data.videoIndex) { // 没有播放时播放视频
			this.setData({
				videoIndex: index
			})
			var videoContext = wx.createVideoContext('video' + index)
			videoContext.play()
		} else {
			//停止正在播放的视频
			var videoContextPrev = wx.createVideoContext('video' + this.data.videoIndex)
			videoContextPrev.stop()
			//将点击视频进行播放
			this.setData({
				videoIndex: index
			})
			var videoContextCurrent = wx.createVideoContext('video' + index)
			videoContextCurrent.play()
		}
	},
  //下拉刷新
  onPullDownRefresh: function () {
    
    console.log('下拉刷新---');
  },
  // 滑动底部加载
  lower: function () {
    console.log('滑动底部加载====');
    var that = this;
    that.setData({
      page: that.data.page + 1
    });

    this.findListPage({
      page: that.data.page
    });
  },
  findListPage:function()
  {
    let that = this;
    let filter = { category: this.data.category };
    wx.showLoading({
      icon:'none',
      title:'加载中'
    });
    wx.cloud.callFunction({
      name: 'pagination',
      data: {
        dbName: 'video_list',
        pageIndex: this.data.page,
        pageSize: 100,
        filter: filter
      },
      success(res) {
        //res.result.data
        wx.hideLoading();
        if (0 == res.result.data.length)
        {
          if(1!=that.data.page)
          {
            // wx.showToast({
            //   icon: 'none',
            //   title: '没有更多了',
            // })
          }
        }else
        {
          let arr0 = that.data.videoList;
          arr0 = arr0.concat(res.result.data); 
          that.setData({
            videoList: arr0
          });
        }
      },
      fail() {
        
      }
    });
  },
  onShareAppMessage: function () {

  }
})